package com.dushougudu;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;

public class Sort {

    int nums[];
    int ans[];

    @Before
    public void init() {
//        nums = new int[]{9, 8, 7, 6, 5, 4, 3, 2, 1};
        nums = new int[]{3, 5, 7, 1, 2, 9, 4};
        ans = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
    }

    public void check() {
        for (int i = 0; i < nums.length; i++) {
            Assert.assertEquals(ans[i], nums[i]);
        }
    }

    public void swap(int[] array, int left, int right) {
        int temp = array[left];
        array[left] = array[right];
        array[right] = temp;
    }

    @Test
    public void bubbleSort() {
        if (nums.length == 0) {
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums.length - i - 1; j++) {
                if (nums[j] > nums[j + 1]) {
                    swap(nums, j, j + 1);
                }
            }
        }
        check();
    }

    @Test
    public void selectionSort() {
        if (nums.length == 0) {
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            int min = i;
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[j] < nums[min]) {
                    min = j;
                }
            }
            swap(nums, i, min);
        }
        check();
    }

    @Test
    public void insertionSort() {
        if (nums.length == 0) {
            return;
        }
        for (int i = 1; i < nums.length; i++) {
            int currentIndex = i;
            int currentVal = nums[i];
            //TODO 通过当前值比较
            while (currentIndex - 1 >= 0 && currentVal < nums[currentIndex - 1]) {
                nums[currentIndex] = nums[currentIndex - 1];
                currentIndex--;
            }
            nums[currentIndex] = currentVal;
        }
        check();
    }

    @Test
    public void shellSort() {
        if (nums.length == 0) {
            return;
        }
        int gap = nums.length >> 1;
        while (gap > 0) {
            for (int i = gap; i < nums.length; i++) {
                int currentIndex = i;
                int currentVal = nums[i];
                while (currentIndex - gap >= 0 && currentVal < nums[currentIndex - gap]) {
                    nums[currentIndex] = nums[currentIndex - gap];
                    currentIndex = currentIndex - gap;
                }
                nums[currentIndex] = currentVal;
            }
            //TODO gap不要忘记除2
            gap >>= 1;
        }
        check();
    }

    @Test
    public void mergeSort() {
        nums = sortMerge(nums);
        check();
    }

    public int[] sortMerge(int array[]) {
        if (array.length < 2) {
            return array;
        }
        int mid = array.length >> 1;
        return merge(sortMerge(Arrays.copyOfRange(array, 0, mid)), sortMerge(Arrays.copyOfRange(array, mid, array.length)));
    }

    public int[] merge(int[] left, int[] right) {
        int result[] = new int[left.length + right.length];
        int leftPoint = 0;
        int rightPoint = 0;
        int index = 0;
        while (index < result.length) {
            if (leftPoint >= left.length) {
                result[index++] = right[rightPoint++];
            } else if (rightPoint >= right.length) {
                result[index++] = left[leftPoint++];
            } else if (left[leftPoint] < right[rightPoint]) {
                result[index++] = left[leftPoint++];
            } else {
                result[index++] = right[rightPoint++];
            }
        }
        return result;
    }

    @Test
    public void quickSort() {
        quick(nums, 0, nums.length - 1);
        check();
    }

    public void quick(int[] nums, int low, int high) {
        //TODO 边界条件遗漏
        if (low > high) {
            return;
        }
        int pivot = nums[low];
        int left = low;
        int right = high;
        while (left < right) {
            //TODO 注意等于的边界条件
            while (left < right && nums[right] >= pivot) {
                right--;
            }

            while (left < right && nums[left] <= pivot) {
                left++;
            }

            if (left < right) {
                swap(nums, left, right);
            }
        }
        swap(nums, left, low);

        quick(nums, low, left - 1);
        quick(nums, left + 1, high);
    }

    @Test
    public void heapSort() {
        for (int i = nums.length / 2 - 1; i >= 0; i--) {
            adjustHeap(nums, i, nums.length);
        }
        for (int i = nums.length - 1; i >= 0; i--) {
            swap(nums, 0, i);
            adjustHeap(nums, 0, i);
        }
        check();
    }

    public void adjustHeap(int[] array, int parent, int length) {
        int leftChild = 2 * parent + 1;
        int parentValue = array[parent];
        while (leftChild < length) {
            if (leftChild + 1 < length && array[leftChild] < array[leftChild + 1]) {
                leftChild++;
            }
            if (parentValue > array[leftChild]) {
                break;
            }

            array[parent] = array[leftChild];

            parent = leftChild;
            leftChild = 2 * parent + 1;
        }
        array[parent] = parentValue;
    }


}
